package com.swust.testDemo;

//import java.util.regex.Pattern;//import 架包
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
//import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.Select;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters; 
import java.util.Arrays;
import java.util.Collection; 


@RunWith(Parameterized.class)//括号内，会寻找@parameters里的数据
public class Sne {
	
	  @Parameters    //定义数据
	  public static Collection data() {
	          return Arrays.asList(new Object[][]{
	        	  {"159","60","23.7"},
	        	  {"180","69","21.3"},
	        	  {"190","60","16.6"}
	          });
	  }
	
	
	
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
  
  private String i_height;  
  private String i_weight; 
  private String i_bmi;

  public Sne(String height, String weight, String bmi) {  //构造函数，里面的数据与parameters里定义的数据对应
      this.i_height = height;  
      this.i_weight = weight;
      this.i_bmi = bmi;
  } 
  
  @Before//执行每个测试用例之前进行的动作
  public void setUp() throws Exception {
   System.setProperty ( "webdriver.firefox.bin" , "E:/Firefox/firefox.exe" );
   System.setProperty("webdriver.firefox.marionette","E:\\softwareTest\\geckodriver\\geckodriver.exe");
    driver = new FirefoxDriver();//驱动，Firefox
    baseUrl = "http://cn.onlinebmicalculator.com/";//网址
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);//给时间限制
  }

//  @Test
//  public void t01() {
//    fail("666");
//  }
//  @Test
//  public void t02() {
//      Assert.assertEquals(true,false);
//  }
//  @Test
//  public void t03() {
//      Assert.assertEquals(true,true);
//  }


  
  @Test
  public void testSne() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.name("f_height")).clear();//通过name寻找元素
    driver.findElement(By.name("f_height")).sendKeys(i_height);//输入
    driver.findElement(By.name("f_weight")).clear();
    driver.findElement(By.name("f_weight")).sendKeys(i_weight);
    driver.findElement(By.name("f_submit")).click();//点击
    
    WebElement element = driver.findElement(By.className("bmi_info")).findElement(By.tagName("span"));
    String bmi = element.getText();
    System.out.print("BMI:" + bmi);
   
    Assert.assertEquals(i_bmi,bmi);//进行比较
  }


  
  @After
  public void tearDown() throws Exception {
    driver.quit();//退出，释放内存
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

   private boolean isElementPresent(By by) {
     try {
       driver.findElement(by);
       return true;
     } catch (NoSuchElementException e) {
       return false;
     }
   }

   private boolean isAlertPresent() {
     try {
       driver.switchTo().alert();
       return true;
     } catch (NoAlertPresentException e) {
       return false;
     }
   }

   private String closeAlertAndGetItsText() {
     try {
       Alert alert = driver.switchTo().alert();
       String alertText = alert.getText();
       if (acceptNextAlert) {
         alert.accept();
       } else {
         alert.dismiss();
       }
       return alertText;
     } finally {
       acceptNextAlert = true;
     }
   }
}
